package com.hfb.mashibing.alip8.interview.iq;

import java.text.DecimalFormat;
import java.util.Scanner;

public class RMBUpLowe {
    final static String[] STR_NUMBER = {"零","壹","貳","叁","肆","伍","陆","柒","捌","玖"};
    final static String[] STR_UNIT = {"","拾","佰","仟","萬",
                                         "拾","佰","仟","亿",
                                         "拾","佰","仟","万"};
    final static String[] STR_UNIT2 = {"角","分","里"};

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (true) {
            System.out.println("请输入一个金额：");
            String s = in.nextLine();

            Double number = Double.parseDouble(s);
            if (number < 0) {
                System.out.println("请输入大于零的数字！");
            } else if (number == 0) {
                System.out.println("零元整！");
            } else {
                String convert = convert(number);
                System.out.println(convert);
            }
        }
    }

    private static String convert(Double d) {
        String strNum = new DecimalFormat("#0.###").format(d);

        if(strNum.indexOf(".") != -1) {
            String num = strNum.substring(0,strNum.indexOf("."));
            if (num.length() > 13){
                System.out.println("数字过大，请重新输入！");
                return "";
            }
        }
        String point = "圆整";
        if (strNum.indexOf(".") != -1){
            point = "圆";
        }
        String res = getInt(strNum)+point+getDec(strNum);

        return res.startsWith("圆") ? res.substring(1) : res;
    }

    private static String getInt(String num) {
        if(num.indexOf(".") != -1){
            num = num.substring(0, num.indexOf("."));
        }
        num = new StringBuilder(num).reverse().toString();
        StringBuilder temp = new StringBuilder();
        for (int i = 0; i < num.length(); i++) {
            temp.append(STR_UNIT[i])
                .append(STR_NUMBER[num.charAt(i)-48]);
        }
        num = temp.reverse().toString();
        num = numReplace(num, "零拾", "零");
        num = numReplace(num, "零佰", "零");
        num = numReplace(num, "零仟", "零");
        num = numReplace(num, "零萬", "零");
        num = numReplace(num, "零亿", "亿");
        num = numReplace(num, "零零", "零");
        num = numReplace(num, "亿萬", "亿");

        return num.lastIndexOf("零") == num.length()-1
            ? num.substring(0,num.length()-1)
            : num;
        }

    private static String getDec(String num) {
        if(num.indexOf(".") == -1){
            return "";
        }
        num = num.substring(num.indexOf(".")+1);
        num = new StringBuilder(num).toString();
        StringBuilder temp = new StringBuilder();
        for (int i = 0; i < num.length(); i++) {
            temp.append(STR_NUMBER[num.charAt(i)-48])
                .append(STR_UNIT2[i]);
        }
        num = temp.toString();
        num = numReplace(num, "零角", "零");
        num = numReplace(num, "零分", "零");
        num = numReplace(num, "零里", "零");
        num = numReplace(num, "零零", "零");

        return num.lastIndexOf("零") == num.length()-1
            ? num.substring(0,num.length()-1)
            : num;
    }

    private static String numReplace(String num, String oldStr, String newStr) {
        while(true){
            if(num.indexOf(oldStr) == -1){
                break;
            }
            num = num.replaceAll(oldStr, newStr);
        }
        return num;
    }

}
